let timer: any
let stopScrollInterval: () => void
let stopScrollOnWheel: () => void

export function useBackTop() {
  timer = setInterval(() => {
    const top = document.documentElement.scrollTop
    const speed = top < 2 ? 2 : top / 20
    document.documentElement.scrollTop -= speed
  }, 10)

  stopScrollInterval = () => {
    if (document.documentElement.scrollTop === 0) {
      clearInterval(timer)
    }
  }

  stopScrollOnWheel = () => clearInterval(timer)

  document.documentElement.addEventListener('scroll', stopScrollInterval)
  document.addEventListener('wheel', stopScrollOnWheel)
}

export function useClearBackTop() {
  document.documentElement.removeEventListener('scroll', stopScrollInterval)
  document.removeEventListener('wheel', stopScrollOnWheel)
}
